﻿namespace ReplayDataFile
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;

    public class DataExporter
    {
        public static ulong Fileflag = 0x20120605162156L;

        public static bool ExportRecordListToCompactFile(string fileName, ICollection<DataRecord> recList, bool isZipRecord)
        {
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            FileStream output = null;
            try
            {
                byte[] buffer2;
                string path = fileName + ".tmp";
                output = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None);
                BinaryWriter writer = new BinaryWriter(output);
                RecordZipper zipper = new RecordZipper();
                byte[] buffer = new byte[0x100];
                BinaryWriter writer2 = new BinaryWriter(new MemoryStream(buffer));
                writer2.Write(Fileflag);
                writer2.Write(recList.Count);
                writer.Write(buffer, 0, buffer.Length);
                if (isZipRecord)
                {
                    foreach (DataRecord record in recList)
                    {
                        DataRecord record2 = zipper.Zip(record, true);
                        writer.Write(record.DataType);
                        writer.Write(record.DataVersion);
                        buffer2 = record2.ToByteBuffer(false, null);
                        writer.Write(buffer2);
                    }
                }
                else
                {
                    foreach (DataRecord record in recList)
                    {
                        writer.Write(record.DataType);
                        writer.Write(record.DataVersion);
                        buffer2 = record.ToByteBuffer(false, null);
                        writer.Write(buffer2);
                    }
                }
                writer.Close();
                GZipUtil.CompressFile(path, fileName);
                File.Delete(path);
            }
            catch (Exception exception)
            {
                if (output != null)
                {
                    output.Close();
                }
                Trace.WriteLine("ExportRecordList error! " + exception.Message);
                return false;
            }
            return true;
        }

        public static bool ExportToBlockFile(string dataPath, string exportedFileName, long recPos, List<int> dataTypeList, DateTime beginTime, DateTime endTime, bool isZipRecord)
        {
            bool flag3;
            ForwordDataReader reader = new ForwordDataReader(dataPath);
            if (!reader.Open())
            {
                return false;
            }
            ICollection<DataTypeInfo> dataTypes = null;
            if ((dataTypeList == null) || (dataTypeList.Count == 0))
            {
                dataTypes = reader.GetDataAllTypes();
            }
            else
            {
                dataTypes = reader.GetDataTypes(dataTypeList);
            }
            BlockFileWriter writer = new BlockFileWriter(exportedFileName, 1, 0x5000, 0x40000, dataTypes.Count, dataTypes, 1L) {
                IsAutoAppendBlock = true
            };
            if (!writer.Open())
            {
                reader.Close();
                return false;
            }
            int recCount = 0x2710;
            reader.SetPosition(beginTime, dataTypeList);
            bool flag = false;
        Label_014B:
            flag3 = true;
            if (!reader.IsReadOver)
            {
                ICollection<DataRecord> is3 = reader.ReadNext(recCount);
                foreach (DataRecord record in is3)
                {
                    if (record.Time >= endTime)
                    {
                        flag = true;
                        break;
                    }
                    if (!writer.WriteRecord(record))
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag && (is3.Count >= recCount))
                {
                    goto Label_014B;
                }
            }
            reader.Close();
            writer.Close();
            return true;
        }

        public static bool ExportToCompactFile(string dataPath, string exportedFileName, long recPos, List<int> dataTypeList, DateTime beginTime, DateTime endTime, bool isZipRecord)
        {
            return ExportToCompactFile(dataPath, exportedFileName, recPos, dataTypeList, beginTime, endTime, isZipRecord, null);
        }

        public static bool ExportToCompactFile(string dataPath, string exportedFileName, long recPos, List<int> dataTypeList, DateTime beginTime, DateTime endTime, bool isZipRecord, List<DataRecord> addList)
        {
            BlockDataReader reader = new BlockDataReader(dataPath);
            if (!reader.Open())
            {
                return false;
            }
            ICollection<DataRecord> recList = reader.Read(dataTypeList, beginTime, endTime);
            if ((recList != null) && (addList != null))
            {
                foreach (DataRecord record in addList)
                {
                    recList.Add(record);
                }
            }
            reader.Close();
            return ExportRecordListToCompactFile(exportedFileName, recList, isZipRecord);
        }
    }
}

